﻿using System;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using QuickModel.QuickModelFrame;

namespace QuickModel.AxisTransModel
{
	[Rebuilder(RebuilderName = "AxisRebuilder")]
	public class AxisRebuilder : IRevitModelRebuilder
	{
		public bool TryRebuildRevitModel(Autodesk.Revit.DB.Document inputDoc, RevitModelRequest inputReqeust, out BuildData buildData)
		{
			buildData = new BuildData();
			AxisRevitModelRequest axisRevitModelRequest = inputReqeust as AxisRevitModelRequest;
			bool flag = axisRevitModelRequest == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				try
				{
					AxisRequest aq = axisRevitModelRequest.Request;
					bool flag2 = aq.AxisCurves.Count > 1;
					if (flag2)
					{
						CurveLoop curveLoop = new CurveLoop();
						foreach (Curve curve in aq.AxisCurves)
						{
							curveLoop.Append(curve);
						}
						ElementId id = inputDoc.ActiveView.SketchPlane.Id;
						ElementId id2 = RevitDocumentExtension.CreateMultiSegmentGrid(inputDoc, aq.AxisType.Id, curveLoop, id);
						Element element = RevitVersionFuncs.GetElement(inputDoc, id2);
						MultiSegmentGrid newGrid = element as MultiSegmentGrid;
						newGrid.Name = aq.Number;
						buildData.UseWork = delegate
						{
							this.HideGridNumber(inputDoc, newGrid, aq.HideNumber);
						};
					}
					else
					{
						bool flag3 = aq.AxisCurves.Count == 1;
						if (flag3)
						{
							Grid grid = null;
							Curve curve2 = aq.AxisCurves[0];
							bool flag4 = curve2 is Arc;
							if (flag4)
							{
								grid = inputDoc.YjkNewGrid(curve2 as Arc);
							}
							else
							{
								bool flag5 = curve2 is Line;
								if (flag5)
								{
									grid = inputDoc.YjkNewGrid(curve2 as Line);
								}
							}
							bool flag6 = grid != null;
							if (flag6)
							{
								grid.ChangeTypeId(aq.AxisType.Id);
								grid.Name = aq.Number;
								this.HideGridNumber(inputDoc, grid, aq.HideNumber);
							}
						}
					}
				}
				catch
				{
					return false;
				}
				result = true;
			}
			return result;
		}

		private void HideGridNumber(Autodesk.Revit.DB.Document doc, Element gridElem, int nHideNumber)
		{
			bool flag = gridElem is Grid;
			if (flag)
			{
				Grid gridItem = gridElem as Grid;
				this.HideGridNumber(doc.ActiveView, gridItem, nHideNumber);
			}
			else
			{
				bool flag2 = gridElem is MultiSegmentGrid;
				if (flag2)
				{
					MultiSegmentGrid multiSegmentGrid = gridElem as MultiSegmentGrid;
					ElementId elementId = multiSegmentGrid.GetGridIds().First<ElementId>();
					ElementId elementId2 = multiSegmentGrid.GetGridIds().Last<ElementId>();
					Grid gridItem2 = doc.GetElementById(elementId) as Grid;
					Grid gridItem3 = doc.GetElementById(elementId2) as Grid;
					bool flag3 = nHideNumber == 0;
					if (flag3)
					{
						this.HideGridNumber(doc.ActiveView, gridItem3, 2);
					}
					else
					{
						bool flag4 = nHideNumber == 1;
						if (flag4)
						{
							this.HideGridNumber(doc.ActiveView, gridItem2, 2);
						}
						else
						{
							bool flag5 = nHideNumber == 2;
							if (flag5)
							{
								this.HideGridNumber(doc.ActiveView, gridItem2, 2);
								this.HideGridNumber(doc.ActiveView, gridItem3, 2);
							}
						}
					}
				}
			}
		}

		private void HideGridNumber(Autodesk.Revit.DB.View activeView, Grid gridItem, int nHideNumber)
		{
			bool flag = nHideNumber == 0;
			if (flag)
			{
				gridItem.HideBubbleInView(0, activeView);
			}
			else
			{
				bool flag2 = nHideNumber == 1;
				if (flag2)
				{
					gridItem.HideBubbleInView(1, activeView);
				}
				else
				{
					bool flag3 = nHideNumber == 2;
					if (flag3)
					{
						gridItem.HideBubbleInView(0, activeView);
						gridItem.HideBubbleInView(1, activeView);
					}
				}
			}
		}
	}
}
